import areaList from '../../config/areaList';
import { wxuuid } from '../../utils/index'
import config from '../../config/index';
const app = getApp();
Page({
  data: {
    companyName: "",
    companyPhone: "",
    legalPerson: "",
    verification: "",
    address: "",
    province: "",
    city: "",
    area: " ",
    street: " ",
    disabled: false,
    downtime: 60000,
    frontFileList: [],
    behindFileList: [],
    areaPicker: false,
    streetPicker: false,
    areaList: areaList,
    streetList: [],
    errMessage: {
      address: "请输入详细地址",
      province: "请选择城市/区",
      city: "请选择城市/区",
      area: "请选择城市/区",
      street: "请选择街道",
      companyName: "请输入企业名称",
      companyPhone: "请输入手机号",
      legalPerson: "请输入真实姓名",
      verification: "请输入手机验证码",
      idCardFrontUrl: "请上传身份证正面",
      idCardBackUrl: "请上传身份证反面"
    }
  },
  onLoad: function (options) {},
  onAreaConfirm(event) {
    console.log(event)
    const {
      values
    } = event.detail;
    let province = values[0].name
    let city = values[1].name
    let area = values[2].name
    let code = values[2].code
    this.setData({
      "province": province,
      "city": city,
      "area": area,
      "street": "",
      "areaPicker": false
    })
    wx.$apis.getStreet(code).then(res => {
      let streetList = [];
      for (let key in res.data) {
        streetList.push({
          code: key,
          name: res.data[key],
          text: res.data[key]
        })
      }
      this.setData({
        streetList: streetList
      })
    })
  },
  onAreaCancel() {
    this.setData({
      "areaPicker": false
    })
  },
  frontAfterRead(event) {
    const {
      file
    } = event.detail;
    let fileList = [];
    wx.$apis.uploadImage(file.url).then(res => {
      fileList.push({
        ...file,
        url: res.data,
        remoteUrl: JSON.parse(res.data).data
      });
      this.setData({
        "frontFileList": fileList,
      });
    })
  },
  behindAfterRead(event) {
    const {
      file
    } = event.detail;
    let fileList = [];
    wx.$apis.uploadImage(file.url).then(res => {
      fileList.push({
        ...file,
        url: res.data,
        remoteUrl: JSON.parse(res.data).data
      });
      this.setData({
        "behindFileList": fileList,
      });
    })
  },
  deleteFront() {
    this.setData({
      "frontFileList": [],
    });
  },
  deleteBehind() {
    this.setData({
      "behindFileList": [],
    });
  },
  pickerArea() {
    this.setData({
      "areaPicker": true
    })
  },
  pickerStreet() {
    this.setData({
      "streetPicker": true
    })
  },
  onStreetConfirm(event) {
    console.log(event)
    const {
      value
    } = event.detail;
    this.setData({
      "street": value.name,
      "streetPicker": false
    })
  },
  onStreetCancel() {
    this.setData({
      "streetPicker": false
    })
  },
  register() {
    let params = {
      companyName: this.data.companyName,
      legalPerson: this.data.legalPerson,
      companyPhone: this.data.companyPhone,
      verification: this.data.verification,
      province: this.data.province,
      city: this.data.city,
      area: this.data.area,
      street: this.data.street,
      address: this.data.address,
      idCardFrontUrl: this.data.frontFileList[0] && this.data.frontFileList[0].remoteUrl || "",
      idCardBackUrl: this.data.behindFileList[0] && this.data.behindFileList[0].remoteUrl || ""
    }
    for (let key in params) {
      if (!params[key]) {
        wx.showToast({
          title: this.data.errMessage[key],
          icon: 'none',
          duration: 2000
        })
        return
      }
    }
    params.userName = this.data.companyPhone
    params.companyEmail = `${this.data.companyPhone}@163.com`
    params.password = wxuuid()
    params.platformType = config.platformType
    wx.showLoading({
      title: '正在注册',
    })
    wx.$apis.registerCompany(params).then(res => {
      wx.reLaunch({
        url: '/pages/login/index',
      })
      wx.showToast({
        title: '请等待审核通过',
        icon: 'none',
        duration: 2000
      })
    })
  },
  sendMsg() {
    if (!this.data.companyPhone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
      title: '发送短信中',
    })
    wx.$apis.sendSms(this.data.companyPhone, 5).then(res => {
      this.setData({
        disabled: true,
      })
      const countDown = this.selectComponent('.control-count-down');
      countDown.start();
    })
  },
  finished() {
    this.setData({
      disabled: false,
      downtime: 60000
    })
  },
})